﻿#include "notifylabel.h"
#include <QPainter>
#include <QTimerEvent>

NotifyLabel::NotifyLabel(QWidget *parent, int msec) : QLabel(parent), defaultMSec(msec > 0 ? msec : 5000)
{
    setWordWrap(true);
    setAlignment(Qt::AlignCenter);
}

void NotifyLabel::showText(const QString &string)
{
    if(string.isEmpty())
    {
        timer.stop();
        hide();
        clear();
    }
    else
    {
        setText(string);
        if(isHidden())
            show();
        else
            update();
        timer.start(defaultMSec, this);
    }
}

void NotifyLabel::timerEvent(QTimerEvent *e)
{
    if(e->timerId() == timer.timerId())
    {
        timer.stop();
        hide();
        clear();
    }
    QLabel::timerEvent(e);
}

void NotifyLabel::paintEvent(QPaintEvent *e)
{
    QLabel::paintEvent(e);
}
